package org.nw.web.sys;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nc.vo.pub.AggregatedValueObject;

import org.apache.commons.lang.StringUtils;
import org.nw.constants.Constants;
import org.nw.constants.FunRegisterConst;
import org.nw.exception.BusiException;
import org.nw.service.sys.FunService;
import org.nw.vo.ParamVO;
import org.nw.vo.TreeVO;
import org.nw.vo.sys.FunVO;
import org.nw.web.AbsTreeFormController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @author xuqc
 * @date 2012-6-16 下午06:23:50
 */
@Controller
@RequestMapping(value = "/fun")
public class FunController extends AbsTreeFormController {

	@Autowired
	private FunService funService;

	@Override
	public FunService getService() {
		return funService;
	}

	@Override
	public String getTreePkField() {
		return FunVO.PK_FUN;
	}

	@Override
	public String getTreeTextField() {
		return FunVO.FUN_NAME;
	}

	@Override
	public String getTreeCodeField() {
		return FunVO.FUN_CODE;
	}

	// @Override
	// protected String getFunHelpName(String funCode) {
	// return "/sys/fun.jsp";
	// }

	@RequestMapping(value = "/getFunTree.json")
	@ResponseBody
	public List<TreeVO> getFunTree(HttpServletRequest request, HttpServletResponse response) {
		String parent_id = request.getParameter("parent_id");
		if(StringUtils.isBlank(parent_id)) {
			parent_id = request.getParameter("node");
		}
		String isRoot = request.getParameter("isRoot");
		if(Constants.TRUE.equals(isRoot)) {
			parent_id = null;
		}
		return this.getService().getOriginalFunTree(parent_id);
	}

	@Override
	protected void checkBeforeSave(AggregatedValueObject billVO, ParamVO paramVO) {
		FunVO vo = (FunVO) billVO.getParentVO();
		if(vo.getFun_code() == null) {
			throw new RuntimeException("功能菜单编码不能为空！");
		}
		FunVO funVO = this.getService().getFunVOByFunCodeWithNoDr(vo.getFun_code());
		if(StringUtils.isBlank(vo.getPrimaryKey())) {
			// 新增的情况
			if(funVO != null) {
				throw new RuntimeException("编码已经存在！");
			}
		} else {
			// 修改的情况
			if(funVO != null) {
				if(!vo.getPrimaryKey().equals(funVO.getPk_fun())) {
					throw new RuntimeException("编码已经存在！");
				}
			}
		}

		if(StringUtils.isNotBlank(vo.getParent_id())) {
			FunVO parentVO = this.getService().getByPrimaryKey(FunVO.class, vo.getParent_id());
			// 1、按钮下不能有子节点
			if(parentVO.getFun_property().intValue() == FunRegisterConst.POWERFUL_BUTTON) {
				throw new BusiException("按钮节点不能创建子节点");
			}
			// 2、web节点只能放在虚功能节点下
			if(vo.getFun_property().intValue() == FunRegisterConst.LFW_FUNC_NODE) {
				if(parentVO.getFun_property().intValue() != FunRegisterConst.INEXECUTABLE_FUNC_NODE) {
					throw new BusiException("web节点只能放在虚功能节点下！");
				}
			}
			// 2、按钮只能放在web节点下
			if(vo.getFun_property().intValue() == FunRegisterConst.POWERFUL_BUTTON) {// 按钮
				if(parentVO.getFun_property().intValue() != FunRegisterConst.LFW_FUNC_NODE) {
					throw new BusiException("按钮只能放在web节点下！");
				}
			}
		}
	}
}
